<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use api\utils\PayApiClient;
use herosphp\core\Log;
use herosphp\core\Loader;
use utils\sms\GetCr6868Info;
Loader::import('utils.sms.GetCr6868Info');

/**
 * 后台管理 index 控制器
 */
class IndexAction extends CommonAction {

    /**
     * 首页方法
     * @param HttpRequest $request
     */
    public function index(HttpRequest $request) {
		//大塑支付用户信息
		$userInfo = $this->__getUserInfo('*');
		$data['userid'] = $userInfo['userid'];
		$url = "api/payment/getBalance";
		$result = PayApiClient::send($url, $data);
		if($result->isSucess()){
			$res = $result->getData();
			$balance = $res['balance'];
		}else{
			Log::info($result->getMessage());
			$balance = 0;
		}
		$balance = setFloat($balance,2);
		$this->assign('userInfo',$userInfo);
		$this->assign('balance',$balance);

		//获取短信
		$service = Beans::get("admin.adminSetting.service");
		$smsUser = $service->getSetting('sms_account');
		$smsPwd = $service->getSetting('sms_password');
		$smRisk = $service->getSetting('sms_risk');
		$smRisk = $smRisk>0?$smRisk:100;
		$smsService = new GetCr6868Info();
		$smsService->set($smsUser,$smsPwd);
		$smsInfo = $smsService->getInfo();
		$this->assign('smsUser',$smsUser);
		$this->assign('smRisk',$smRisk);
		$this->assign('smsBalance',$smsInfo['balance']);



		$to_start = date('Y-m-d',time()) . " 00:00:00";
		$week_start = date('Y-m-d',strtotime("-".(date('w') - 1)."days")) . " 00:00:00";
		$mon_start = date('Y-m-d',strtotime("-".(date('d') - 1)."days")) . " 00:00:00";

		$memberService = Beans::get('member.member.service');
		$productService = Beans::get('product.product.service');
		$orderService = Beans::get('trading.tradingOrder.service');
		$orderModel = Loader::model('tradingOrder');

		//商品
		$to_productTotal = $productService->count(array('is_delete'=>0,'addtime' => array('>'=>$to_start)));
		$week_productTotal = $productService->count(array('is_delete'=>0,'addtime' => array('>'=>$week_start)));
		$mon_productTotal = $productService->count(array('is_delete'=>0,'addtime' => array('>'=>$mon_start)));
		$productTotal = $productService->count(array('is_delete'=>0));
		$this->assign('to_productTotal',$to_productTotal ? $to_productTotal : 0);
		$this->assign('week_productTotal',$week_productTotal ? $week_productTotal : 0);
		$this->assign('mon_productTotal',$mon_productTotal ? $mon_productTotal : 0);
		$this->assign('productTotal',$productTotal ? $productTotal : 0);

		//订单
		//待卖家确认总量
		$merchant_confirmTotal = $orderService->count(array('order_status'=>$orderModel::WAIT_FOR_MERCHANT_CONFIRMATION));
		//待确认收款总量
		$pendIngReceipt = $orderService->count(array('order_status'=>$orderModel::ORDER_STATUS_UNPAY,'pay_way'=>$orderModel::PAY_WAY_OFFLINE));
		//代发货总量
		$noShippedTotal = $orderService->count(array('order_status'=>$orderModel::ORDER_STATUS_TO_BE_SHIPPED));
		$this->assign('merchant_confirmTotal',$merchant_confirmTotal ? $merchant_confirmTotal : 0);
		$this->assign('pendIngReceipt',$pendIngReceipt ? $pendIngReceipt : 0);
		$this->assign('noShippedTotal',$noShippedTotal ? $noShippedTotal : 0);

		//会员
		$to_member = $memberService->getTotal($to_start);
		$week_member = $memberService->getTotal($week_start);
		$mon_member = $memberService->getTotal($mon_start);
		$member = $memberService->getTotal();
		$this->assign('to_member',$to_member ? $to_member : 0);
		$this->assign('week_member',$week_member ? $week_member : 0);
		$this->assign('mon_member',$mon_member ? $mon_member : 0);
		$this->assign('member' ,$member ? $member : 0);

        $url = $request->getParameter('url', 'trim');
        if ( !$url ) $url = "/admin/index/form";
        $this->setView("index");
        $this->assign("indexPage", $url);
    }

	public function getSellDate(HttpRequest $request){
		$type = $request->getParameter('type');
		$days = $request->getParameter('days');
		$days = $days-1;
		if($days>7){
			$days = 30;
		}else{
			$days = 7;
		}
		$day = date('Y-m-d', strtotime("-{$days} day"));

		switch($type){
			case 'amount':
				$orderService = Beans::get('trading.tradingOrder.service');
				$items = $orderService->getOrderSalesVolumeForDays($day);
				$new_items = array();
				foreach($items as $v){
					$new_items[$v['days']] = $v['amount'];
				}
				$time_arr[] =  date('Y-m-d', strtotime("-{$days} day"));
				for($i=1;$i<=$days;$i++){
					$new_day = date('Y-m-d',strtotime("+{$i}day",strtotime($day)));
					$time_arr[] = $new_day;
				}
				$data = array();
				foreach($time_arr as $v){
					$data['x'][] = $v;
					$data['y'][] = $new_items[$v]?$new_items[$v]:'0.00';
				}
				AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS,'success',$data);
				break;
			case 'number':
				$orderService = Beans::get('trading.tradingOrder.service');
				$items = $orderService->getQuantityForDays($day);
				$new_items = array();
				foreach($items as $v){
					$new_items[$v['days']] = $v['num'];
				}
				$time_arr[] =  date('Y-m-d', strtotime("-{$days} day"));
				for($i=1;$i<=$days;$i++){
					$new_day = date('Y-m-d',strtotime("+{$i}day",strtotime($day)));
					$time_arr[] = $new_day;
				}
				$data = array();
				foreach($time_arr as $v){
					$data['x'][] = $v;
					$data['y'][] = $new_items[$v]?$new_items[$v]:'0';
				}
                AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS,'success',$data);
				break;
			case 'ranking':
				$saleService = Beans::get('trading.tradingSale.service');
				$items = $saleService->searchByRanking(10,1,array('start_time'=>$day));
				$data = array();
				if($items['items']){
					foreach($items['items'] as $v){
						$data['x'][] = $v['product_name'];
						$data['y'][] = $v['total_sale_quantity'];
					}
					AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS,'success',$data);
				}else{
					AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,'暂无数据');
				}

				break;
			default :
				break;
		}
	}

    /**
     * 首页方法 适用于default模板
     * @param HttpRequest $request
     */
    public function index_old(HttpRequest $request) {
		$week_start = date('Y-m-d',strtotime("-".(date('w') - 1)."days")) . " 00:00:00";
		$mon_start = date('Y-m-d',strtotime("-".(date('d') - 1)."days")) . " 00:00:00";
		$memberService = Beans::get('member.member.service');
		$productService = Beans::get('product.product.service');
		$orderService = Beans::get('trading.tradingOrder.service');
		$saleWeight = $orderService->getQuantityTotal();

		$week_order = $orderService->getTotal(1,$week_start);
		$mon_order = $orderService->getTotal(1,$mon_start);
		$order = $orderService->getTotal(1);

		$week_sale = $orderService->getTotal(2,$week_start);
		$mon_sale = $orderService->getTotal(2,$mon_start);
		$sale = $orderService->getTotal(2);

		$week_member = $memberService->getTotal($week_start);
		$mon_member = $memberService->getTotal($mon_start);
		$member = $memberService->getTotal();
		$productTotal = $productService->count(array('is_delete'=>0));
		$rs = $productService->getItem(array('is_delete'=>0),'sum(quantity-freeze_quantity) as qty');
		$totalWeight = $rs['qty'];

		$this->assign('totalWeight',$totalWeight ? $totalWeight  : '0.00');
		$this->assign('productTotal',$productTotal ? $productTotal : 0);

		$this->assign('week_member',$week_member ? $week_member : 0);
		$this->assign('mon_member',$mon_member ? $mon_member : 0);
		$this->assign('member' ,$member ? $member : 0);
		$this->assign('week_order',$week_order ? $week_order : 0);
		$this->assign('mon_order',$mon_order ? $mon_order : 0);
		$this->assign('order',$order ? $order     : 0);

		$this->assign('week_sale',$week_sale ? $week_sale : '0.00');
		$this->assign('mon_sale' , $mon_sale ? $mon_sale : '0.00');
		$this->assign('sale' ,$sale  ? $sale : '0.00');
		$this->assign('saleWeight' ,$saleWeight ? $saleWeight : '0.00');

        $url = $request->getParameter('url', 'trim');
        if ( !$url ) $url = "/admin/index/form";
        $this->setView("index");
        $this->assign("indexPage", $url);

    }

    //添加数据
    public function insert($data) {
        AjaxResult::ajaxSuccessResult();
    }

    public function clist(HttpRequest $request) {

        $this->setView("clist");

    }

    public function remove(HttpRequest $request) {
        AjaxResult::ajaxSuccessResult();
    }

    public function edit(HttpRequest $request) {
        $this->setView("edit");
    }

    public function error(HttpRequest $request) {
        //$this->showMessage($request->getParameter("type"), $request->getParameter('message', 'base64_decode'));
		$this->showMessage($request->getParameter("type"), $request->getParameter('message'));

	}
  
}
